package com.trkj.controller.cf;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.pojo.CertificateType;
import com.trkj.pojo.StaffInformation;
import com.trkj.pojo.ZszlType;
import com.trkj.service.cf.CertificateService;
import com.trkj.service.cf.CertificateTypeService;
import com.trkj.service.cf.ZszlTypeService;
import com.trkj.vo.cf.CertificateTypeVo;
import com.trkj.vo.cf.CertificateVo;

@Controller
public class CultivateController {
		@Autowired
		private CertificateService certificateService;
		@Autowired
		private ZszlTypeService zszlType;
		@Autowired
		private CertificateTypeService certificateTypeService;
	/**
	 * 证书
	 */
	 @RequestMapping("Certificate_pages")
		@ResponseBody
		public Map<String, Object> CoursePage(int page, int rows) {
			Page<JSONArray> pg=PageHelper.startPage(page, rows);
			Map<String, Object> map = new HashMap<>();
			List<JSONArray> list=pg.getResult();
			map.put("total", certificateService.coursePage().size()); 
			map.put("rows", list);
			return map;
		}
	// 新增
		@GetMapping("Certificate_add")
		@ResponseBody
		public void CourseEnroll(Long zszlId,String certiName,String certiOwnertype,
				String certiBorrowstate,String certiUsestate,Timestamp certiEffectivedate,HttpSession session) {
			System.out.println(zszlType+"_"+certiName+"_"+certiOwnertype+"_"+
					certiBorrowstate+"_"+certiUsestate+"_"+certiEffectivedate);
				certificateService.Certificateadd(zszlId,certiName,certiOwnertype,certiBorrowstate
						,certiUsestate,certiEffectivedate==null?(Timestamp) new Date():certiEffectivedate,session);
		}
		//删除
		@PostMapping("Certificate_remove")
		@ResponseBody
		public void Certificateremove(Long certiId) {
			certificateService.certificateremove(certiId);
		}
		//证书添加	
		@GetMapping("login_certificate_add")
		public String login81(HttpSession session,Model m){
			StaffInformation ff = (StaffInformation) session.getAttribute("staff");
			m.addAttribute("staff",ff.getStainName());
			return "certficate_add";
		}
		//查询证书种类
		@GetMapping("certificate_query1")
		@ResponseBody
		public List<ZszlType> zszlTypePage1() {
			List<ZszlType>	list= zszlType.findAll1();
			 return list;
		}		
		//查询证书种类
		@GetMapping("certificate_query")
		@ResponseBody
		public Map zszlTypePage(int page, int rows,String zszlName) {
			long total =zszlType.pageCount();
			List<CertificateVo> list =  zszlType.findAll(page, rows,zszlName);
			Map map = new HashMap<>();
			map.put("total", total); 
			map.put("rows", list);
			return map;
		}
		@PostMapping("uploading")
		@ResponseBody
		public Map  upload(@RequestParam("f") CommonsMultipartFile f,
				HttpServletRequest request) throws IllegalStateException, IOException{
				//将上传的文件保存到工程目录的uploads子目录下
				ServletContext application = request.getServletContext();
				//获取工程所在的物理路径，并添加子目录uploads
				String path = application.getRealPath("");
				System.out.println(path);
				//创建uploads文件夹
				File dir = new File(path+"\\WEB-INF\\images\\uploads");
				if(!dir.exists()) dir.mkdirs();
				//获取原始文件的扩展名
				String originalFileName = f.getOriginalFilename();
						int pos = originalFileName.lastIndexOf('.');//a.b.c.jpg
						
				String ext = originalFileName.substring(pos);//获取到扩展名.jpg
				//生成唯一的新文件名
				String newFileName = UUID.randomUUID().toString().toUpperCase() + ext;
				//转存
				f.transferTo(new File(dir, newFileName));//将文件转存到指定的位置
					Map  map =new HashMap<>();
						map.put("file", "/hr/images/uploads/"+newFileName);
					return map ;
		}
		/**
		 * 证书分类
		 */
		@GetMapping("certificateclassify_pages")
		@ResponseBody
		public Map certificateTypePage(String certypeName,int page, int rows) {
			long total =certificateTypeService.pageCount();
			List<CertificateTypeVo> list = certificateTypeService.certificateTypePage(certypeName,page, rows);
			Map map = new HashMap<>();
			map.put("total", total); 
			map.put("rows", list);
			return map;
		}
		//证书分类添加	
		@GetMapping("certificateclassify_add")
		@ResponseBody
		public void certificateclassify(CertificateTypeVo vo){
			 System.out.println(vo.getCertypeId());
			certificateTypeService.certificateTypeadd(vo); 
		}		
		//编辑
		@GetMapping("certificateclassify_update")
		@ResponseBody
		public void certificateclassifyUpdate(CertificateTypeVo vo){
			 System.out.println(vo.getCertypeId());
			certificateTypeService.certificateTypeUpdate(vo); 
		}	
		//删除
				@PostMapping("certificateclassify_remove")
				@ResponseBody
				public void certificateclassifyremove(Long certypeId) {
					certificateTypeService.certificateclassifyremove(certypeId);
				}
}
